﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;


  public  class DataTableHelper
    {
        #region Datatable的查询，更新方法
        /// <summary>
        /// 对Datatable进行where搜索，并返回新的Datatable
        /// </summary>
        /// <param name="dt_">要进行搜索的Datatable</param>
        /// <param name="selectSQL_">搜索SQL语句</param>
        /// <param name="distinct_col">不重复的字段（为空则不限制）</param>
        /// <returns>搜索过之后的新Datatable</returns>
        public static DataTable dt_select(DataTable dt_, string selectSQL_, string distinct_col)
        {
            DataTable dt_return = dt_.Clone();
            string distinct_col_valueStr = "";//存放已经读取的字段值
            DataRow[] drA = dt_.Select(selectSQL_, "id desc");
            for (int i = 0; i < drA.Length; i++)
            {
                if (distinct_col != "")
                {
                    string thisRow_Value = DataTypePlus.requestString(drA[i][distinct_col]);
                    if (("," + distinct_col_valueStr + ",").IndexOf("," + thisRow_Value + ",") == -1)//检测该值是否已经读取
                    {
                        if (distinct_col_valueStr != "") distinct_col_valueStr += ",";
                        distinct_col_valueStr += thisRow_Value;
                        dt_return.ImportRow((DataRow)drA[i]);
                    }
                }
                else dt_return.ImportRow((DataRow)drA[i]);

            }
            return dt_return;
        }
        public static DataTable dt_select(DataTable dt_, string selectSQL_, string distinct_col, string sort)
        {
            DataTable dt_return = dt_.Clone();
            string distinct_col_valueStr = "";//存放已经读取的字段值
            DataRow[] drA = dt_.Select(selectSQL_, sort);
            for (int i = 0; i < drA.Length; i++)
            {
                if (distinct_col != "")
                {
                    string thisRow_Value = DataTypePlus.requestString(drA[i][distinct_col]);
                    if (("," + distinct_col_valueStr + ",").IndexOf("," + thisRow_Value + ",") == -1)//检测该值是否已经读取
                    {
                        if (distinct_col_valueStr != "") distinct_col_valueStr += ",";
                        distinct_col_valueStr += thisRow_Value;
                        dt_return.ImportRow((DataRow)drA[i]);
                    }
                }
                else dt_return.ImportRow((DataRow)drA[i]);

            }
            return dt_return;
        }

        /// <summary>
        /// 通过新的行DataRow,对Datatable的某条Datarow进行更新，并返回新的Datatable
        /// </summary>
        /// <param name="dt_">要进行更新的Datatable</param>
        /// <param name="dr_i_">要进行更新的第i行</param>
        /// <param name="dr_New">新的行</param>
        /// <returns>更新后的Datatable</returns>
        public static DataTable dt_update(DataTable dt_, int dr_i_, DataRow dr_New)
        {
            DataRow dr_needUpdate = dt_.Rows[dr_i_];
            dr_needUpdate.BeginEdit();
            foreach (DataColumn c in dt_.Columns) dr_needUpdate[c.ColumnName] = dr_New[c.ColumnName];
            dr_needUpdate.EndEdit();
            return dt_;
        }
       
        /// <summary>
        /// 通过字段名和值，获取所在Datatable的行数
        /// </summary>
        /// <param name="dt_">要查询的Datatable</param>
        /// <param name="keyname_">字段名</param>
        /// <param name="keyvalue_">值</param>
        /// <returns>第几行</returns>
        public static int get_dr_iBYkeyid(DataTable dt_, string keyname_, string keyvalue_)
        {
            int int_return = 0;
            for (int i = 0; i < dt_.Rows.Count; i++)
            {
                DataRow dr_ = dt_.Rows[i];
                if (DataTypePlus.requestString(dr_[keyname_]) == keyvalue_)
                {
                    int_return = i;
                    break;
                }
            }
            return int_return;
        }
      /// <summary>
      /// 添加
      /// </summary>
      /// <param name="dt_">原dt</param>
      /// <param name="dt_new">要添加的dt（dr集合）</param>
      /// <returns></returns>
        public static DataTable Add(DataTable dt_, DataTable dt_new)
        {
            foreach (DataRow dr in dt_new.Rows)
            {
                dt_.ImportRow(dr);
            }
            return dt_;
        }
        #endregion

        #region 过滤datatable的列
        /// <summary>
        /// 过滤datatable的列（保留设定的列）
        /// </summary>
        /// <param name="dt_"></param>
        /// <param name="ColumnsA">要保留的列</param>
        /// <returns></returns>
        public static DataTable dt_byColumns(DataTable dt_, string[] ColumnsA)
        {
            for (int i = dt_.Columns.Count - 1; i >= 0; i--)
            {
                DataColumn dc = dt_.Columns[i];
                string thisColumnName = dc.ColumnName;
                if (StringPlus.inAi(ColumnsA, thisColumnName) < 0) dt_.Columns.Remove(thisColumnName);
            }

            return dt_;
        }
        /// <summary>
        /// 过滤datatable的列（删除设定的列）
        /// </summary>
        /// <param name="dt_"></param>
        /// <param name="ColumnsA">要删除的列</param>
        /// <returns></returns>
        public static DataTable dt_byColumns_del(DataTable dt_, string[] ColumnsA)
        {
            foreach (string col in ColumnsA) dt_.Columns.Remove(col);

            return dt_;
        }
        #endregion
    }

